package com.example.greentrack;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class CarbonActivity extends AppCompatActivity {
    private EditText etCO2;
    private TextView tvResult, tvCurrentPoints;
    private Button btnCalculate, btnBack;
    private double exchangeRate = 0.15;
    private double currentPoints = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carbon);

        initViews();

        Intent intent = getIntent();
        double currentCO2 = intent.getDoubleExtra("current_co2", 0);
        currentPoints = intent.getDoubleExtra("current_points", 0);

        etCO2.setText(String.valueOf(currentCO2));
        tvCurrentPoints.setText(String.format("当前积分: %.1f", currentPoints));

        btnCalculate.setOnClickListener(v -> calculateCarbonPoints());
        btnBack.setOnClickListener(v -> finish());
    }

    private void initViews() {
        etCO2 = findViewById(R.id.et_co2);
        tvResult = findViewById(R.id.tv_result);
        tvCurrentPoints = findViewById(R.id.tv_current_points);
        btnCalculate = findViewById(R.id.btn_calculate);
        btnBack = findViewById(R.id.btn_back);
    }
    private void calculateCarbonPoints() {
        try {
            double co2 = Double.parseDouble(etCO2.getText().toString());
            double newPoints = co2 * exchangeRate;

            String resultText = String.format(
                    "新增积分: %.1f\n\n感谢您为环保做出的贡献!",
                    newPoints
            );
            tvResult.setText(resultText);
            tvResult.setVisibility(View.VISIBLE);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }



}